class_name ComboView
extends BaseView

## TODO 通过open_view参数，打开界面时自动打开一些界面

#region 属性
@export var combo_view_btn_scene:PackedScene
@export var main_type_items:Array[ComboViewMainTypeItem]
@onready var main_type: Panel = %MainType
@onready var sub_type: Panel = %SubType
@onready var item_list: Panel = %ItemList
@onready var info: Panel = %Info
@onready var main_type_container: VBoxContainer = %MainTypeContainer
@onready var sub_type_container: VBoxContainer = %SubTypeContainer
@onready var item_list_container: VBoxContainer = %ItemListContainer
@onready var info_container: VBoxContainer = %InfoContainer
var _cur_type_btns:Array[ComboViewBtn] = []
# 当前选中的
var _cur_main_type_btns:Array[ComboViewBtn] = []
# 所有生成的
var _main_type_btns:Array[ComboViewBtn] = []
# 当前选中的
var _cur_sub_type_btns:Array[ComboViewBtn] = []
# 所有生成的
var _sub_type_btns:Array[ComboViewBtn] = []
var args:Dictionary = {}
#endregion
#region 字段
## 打开info界面的计数，>0会打开info，==0会关闭info
var info_open_count:int = 0:
	set(value):
		info_open_count = value
		info.visible = info_open_count > 0
#endregion
#region 信号
#endregion

#region 基类方法
func init_view():
	super.init_view()
func on_enter(args:Dictionary = {}):
	super.on_enter(args)
	_cur_type_btns.clear()
	_cur_main_type_btns.clear()
	_cur_sub_type_btns.clear()
	_clear_main_type_container()
	_clear_sub_type_container()
	_clear_item_list_container()
	_clear_info_container()
	info_open_count = 0
	self.args = args
	for item in main_type_items:
		add_main_type_btn(item,null)
	GameManager.translate_game_state(GameEnum.GameState.Pausing)
func on_pause(args:Dictionary = {}):
	super.on_pause(args)
func on_resume(args:Dictionary = {}):
	super.on_resume(args)
func on_exit(args:Dictionary = {}):
	super.on_exit(args)
func get_view_state() -> GameEnum.ViewState:
	return super.get_view_state()
#endregion
#region 公共方法
func add_main_type_btn(item:ComboViewMainTypeItem,on_click):
	if not item.check_game_mode(args) or not item.check_game_state(args):
		return
	var btn:ComboViewBtn = combo_view_btn_scene.instantiate() as ComboViewBtn
	main_type_container.add_child(btn)
	btn.init_btn(item)
	btn.pressed.connect(func ():
		_on_main_type_btn_pressed(btn)
		if on_click:
			on_click.call()
		)
	_main_type_btns.append(btn)
func add_sub_type_btn(item:ComboViewSubTypeItem,on_click):
	if not item.check_game_mode(args) or not item.check_game_state(args):
		return
	var btn:ComboViewBtn = combo_view_btn_scene.instantiate() as ComboViewBtn
	sub_type_container.add_child(btn)
	btn.init_btn(item)
	btn.pressed.connect(func ():
		_on_sub_type_btn_pressed(btn)
		if on_click:
			on_click.call()
		)
	_sub_type_btns.append(btn)
func add_item_list_item(item):
	item_list_container.add_child(item)
func press_btn_by_type_name(main_type_name:String,sub_type_name:String = ""):
	var btn:ComboViewBtn = _main_type_btns.filter(func (x):return x.item.type_name == main_type_name)[0]
	btn.button_pressed = not btn.button_pressed
	if sub_type_name != "":
		btn = _sub_type_btns.filter(func (x):return x.item.type_name == main_type_name)[0]
		btn.button_pressed = not btn.button_pressed
func set_info_visible(target_visible:bool):
	# TODO 添加一个过渡动画
	info_open_count = max(0,info_open_count + (1 if target_visible else -1))
#endregion
#region 私有方法
func _clear_main_type_container():
	_main_type_btns.clear()
	for child in main_type_container.get_children():
		child.queue_free()
func _clear_sub_type_container():
	_sub_type_btns.clear()
	for child in sub_type_container.get_children():
		child.queue_free()
func _clear_item_list_container():
	for child in item_list_container.get_children():
		child.queue_free()
func _clear_info_container():
	for child in info_container.get_children():
		child.queue_free()
func _update_view():
	_clear_item_list_container()
	_clear_info_container()
	var filter_btns = _filter_btns(_cur_type_btns)
	if _cur_main_type_btns.size() == 0 or _cur_sub_type_btns.size() == 0:
		_clear_sub_type_container()
	for btn in filter_btns:
		btn.item.excute(self)
func _filter_btns(btns:Array[ComboViewBtn]) -> Array[ComboViewBtn]:
	var filter_btns:Array[ComboViewBtn] = []
	for btn in btns:
		if btn.item is ComboViewMainTypeItem:
			if btn.item.sub_type_items.all(func (x): return not _cur_type_btns.any(func (y):return y.item == x)):
				filter_btns.append(btn)
		elif btn.item is ComboViewSubTypeItem:
			filter_btns.append(btn)
	return filter_btns
func _on_main_type_btn_pressed(main_type_btn:ComboViewBtn):
	if _cur_main_type_btns.has(main_type_btn):
		_cur_main_type_btns.erase(main_type_btn)
		_cur_type_btns.erase(main_type_btn)
		var remove_btns = _cur_sub_type_btns.filter(func (x):return main_type_btn.item.sub_type_items.any(func (y):return y == x))
		for btn in remove_btns:
			btn.button_pressed = not btn.button_pressed
			btn.item.cancel()
			_cur_sub_type_btns.erase(btn)
			_cur_type_btns.erase(btn)
		main_type_btn.item.cancel()
	else:
		if main_type_btn.item.can_multi_select and _cur_main_type_btns.all(func (x):return x.item.can_multi_select):
			pass
		else:
			for btn in _cur_sub_type_btns:
				btn.button_pressed = not btn.button_pressed
				btn.item.cancel()
				_cur_type_btns.erase(btn)
			_cur_sub_type_btns.clear()
			for btn in _cur_main_type_btns:
				btn.button_pressed = not btn.button_pressed
				btn.item.cancel()
				pass
			_cur_main_type_btns.clear()
			_cur_type_btns.clear()
		_cur_main_type_btns.append(main_type_btn)
		_cur_type_btns.append(main_type_btn)
	_update_view()
func _on_sub_type_btn_pressed(sub_type_btn:ComboViewBtn):
	if _cur_sub_type_btns.has(sub_type_btn):
		sub_type_btn.item.cancel()
		_cur_sub_type_btns.erase(sub_type_btn)
		_cur_type_btns.erase(sub_type_btn)
	else:
		if sub_type_btn.item.can_multi_select and _cur_sub_type_btns.all(func (x):return x.item.can_multi_select):
			pass
		else:
			for btn in _cur_sub_type_btns:
				btn.button_pressed = not btn.button_pressed
				btn.item.cancel()
				_cur_type_btns.erase(btn)
			_cur_sub_type_btns.clear()
		_cur_sub_type_btns.append(sub_type_btn)
		_cur_type_btns.append(sub_type_btn)
	_update_view()
#endregion
#region 生命周期函数
func _process(delta: float) -> void:
	# TODO 暂时这么干，后续再改
	pass
	#if GameViewSystem.is_view_open(GameEnum.ViewType.ComboView) and Input.is_action_just_pressed("ComboView"):
		#GameViewSystem.close_view(GameEnum.ViewType.ComboView)
#endregion
